<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	function __construct(){		
		parent::__construct();		
	}
	
	
	public function index()
	{		
		$this->load->helper(array('form','url'));		
		$this->load->view('login');		
	}
	
	public function autenticate(){
		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('username', 'Usuario', 'trim|required|xss_clean');
		$this->form_validation->set_rules('password', 'Contrase&ntilde;a', 'trim|required|xss_clean|callback_validate_credentials');

		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('login');
		}
		else
		{
			$data['cargar_modulo_administrador'] = $this->validate();
			$this->load->view('main', $data);
		}
	}
	
	private function validate(){
		$role_id = $this->session->userdata('role_id');
		$modulo_admin = "";
		if ($role_id==1){
			$modulo_admin = "<div class='section'>
                        <h3><a href='http://localhost/Sistema360/index.php/main/load_module/admin'>Administrador </a></h3>
                        <p><a href='http://localhost/Sistema360/index.php/main/load_module/admin'><img src='http://localhost/Sistema360/common/theme/img/Configuracion.jpg' width='191' height='150' alt='' /></a></p>
                        <p align='justify' style='width: 187px;'>En este m&oacute;dulo, el administrador podr&aacute; configurar los diferentes componentes del sistema</p>
                        </div> <!-- /section -->";
		}
	
		return $modulo_admin;
	}
	
	function validate_credentials($password){
		$this->load->model('vo/UserVO');
		$this->load->model('dao/UserDAO');		
		
		$username = $this->input->post('username');		
		if ($username != ''){			
			//verificacion en base de datos
			$this->UserVO->usuario = $username;
			$this->UserVO->contrasena = md5($password);
			
			return $this->UserDAO->autentication($this->UserVO);
			
		} else {
			$this->form_validation->set_message('validate_credentials', 'Usuario o Contrase&ntilde;a Invalida');
			return false;
		}
		
				
	}
	
		
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

?>